clear all
clear matrix
version 13
capture log close
set more off, permanently

*-------------------------------------------------------------------------------
* Options	
*-------------------------------------------------------------------------------
	global main		= "\\iab.baintern.de\DFS\017\Ablagen\D01700-Data\N2066017\Datenaustausch\GradAB\Schnitzlein_Stueber\Stueber\CADAL_Flow_Dataset\Federica"
	global data_orig= "N:\Ablagen\D01700-IAB-Projekte\D01700-CADAL_Flow_Dataset\package"
	global data_proc= "$main"+"\"+"temp"
	global log   	= "$main"+"\"+"log"
	global do		= "$main"+"\"+"do"
	global data_add = "$main"+"\"+"data"
*-------------------------------------------------------------------------------
cd "$main"
*-------------------------------------------------------------------------------
global niter = 15		/* parameters to be changed */
global date = "2306"	
local year = 2020
********************************************************************************
use "$data_proc\data_federica_1990_2014_west.dta", clear 

gen quarter = q - 4 * (a - 1)
rename st_reg employment
replace employment = employment/1
drop if employment==.|employment==0

gen count_all = 1
gen count_new = 0
replace count_new = 1 if q == founded_q

gen eventdate=yq(year,quarter)
format eventdate %tq
*drop year quarter // HS
drop quarter

ren district krs
merge m:1 krs using "$data_add/amr_krs_72.dta"
drop if _merge!=3
drop _merge

rename w93_imp w93_3
drop if w93_3>=750 & w93_3<900	
drop if w93_3<150
drop if year > 2004
save "$data_proc\data_1990_2004.dta", replace
********************************************************************************
forv ii = 1(1)$niter {

use ${data_add}/interval`ii'_${date}.dta, clear
egen id = group(amr eventdate)
reshape long v, i(id) j(s)
compress 
sum s
global S = `r(max)'
reshape wide v, i(id) j(s)
drop id

qui: merge 1:m amr eventdate using "$data_proc\data_1990_2004.dta"
tab _merge
drop if _merge!=3
drop _merge
compress

********************************************************************************
* Please, always keep just the variables you need!
drop betnr q a year krs w93 w03 w08 w93_3 wz_change state founded_q shut_q
drop num_all dw_imp_mean dw_imp_p50 in_eop out_eop st_nml count_all count_new
********************************************************************************

forv s = 1/$S {

gen check`s' = 0

if `s'==1{
replace check`s' = 1 if employment<=v`s'
}
else if `s'==$S{
local x = `s'-1
replace check`s' = 1 if employment>v`x'
}
else{
local x = `s'-1
replace check`s' = 1 if employment<=v`s' & employment>v`x'
}

by amr eventdate, sort: egen tot_empl`s'=total(check`s')
}

keep amr eventdate tot_empl* v*
duplicates drop 


forv s = 1/$S {
gen aux`s' = 0
replace aux`s' = 1 if v`s'>0
}

forv s = 1/$S {
replace tot_empl`s'=tot_empl`s'*aux`s'
}

compress
saveold "$data_proc/tosend_${date}`year'_`ii'.dta", replace version(14)
}
clear
********************************************************************************
